package com.prj.ufdm.cloud.svr.gateway.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.prj.ufdm.cloud.svr.gateway.bpo.AuthBPO;
import com.prj.ufdm.cloud.svr.gateway.constant.WebMsUrlConstants;
import com.prj.ufdm.cloud.svr.gateway.filter.AuthGatewayFilter;

@Configuration
public class GatewayConfig {  
	
	@Autowired
	AuthBPO authBPO;
	
	@Bean
	public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
		
		return builder.routes()
				.route(
				    "MS_AUTH_URI",
					r -> r.path("/api/auth/**").filters(f -> f.
					hystrix(c -> c.setName("slowcmd").setFallbackUri("forward:/hystrixfallback")).
					filter(new AuthGatewayFilter(authBPO))).
					uri("lb://" + WebMsUrlConstants.MS_AUTH_URI)
				)
				.route(
				    "MS_ORDER_URI",
					r -> r.path("/api/order/**").filters(f -> f.
							hystrix(c -> c.setName("slowcmd").setFallbackUri("forward:/hystrixfallback")).
							rewritePath("/api/order/XXXX", "/api/order/test").
							filter(new AuthGatewayFilter(authBPO))).
					uri("lb://" + WebMsUrlConstants.MS_ORDER_URI)
				)
				.route(
				    "MS_WAREHOUSE_URI",
					r -> r.path("/api/warehouse/**").filters(f -> f.
					    hystrix(c -> c.setName("slowcmd").setFallbackUri("forward:/hystrixfallback")).
					    filter(new AuthGatewayFilter(authBPO))).
					uri("lb://" + WebMsUrlConstants.MS_WAREHOUSE_URI)
				)
				.build();
	}

}
